package com.skyable.auth.exceptions;


import com.skyable.common.common.ResponseResult;
import com.skyable.common.constants.exception.CommonExcpConstants;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
import org.springframework.http.converter.HttpMessageNotReadableException;
import org.springframework.web.HttpMediaTypeNotSupportedException;
import org.springframework.web.HttpRequestMethodNotSupportedException;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.servlet.NoHandlerFoundException;

/**
 * @author GaoYuan
 * @date 2019/11/6 14:17
 * @desc 全局异常处理类
 */
@ControllerAdvice
@ResponseBody
@Slf4j
public class DefaultExceptionHandler {

	/**
	 * 400-Bad Request
	 * @param e
	 * @return
	 */
	@ResponseStatus(HttpStatus.BAD_REQUEST)
	@ExceptionHandler(HttpMessageNotReadableException.class)
	public ResponseResult handleHttpMessageNotReadableException(HttpMessageNotReadableException e) {
		log.error("http message not readable exception ", e);
		return ResponseResult.error(CommonExcpConstants.ERROR_CODE_BAT_REQUEST, CommonExcpConstants.ERROR_MSG_BAT_REQUEST);
	}

	@ResponseStatus(HttpStatus.BAD_REQUEST)
	@ExceptionHandler(MethodArgumentNotValidException.class)
	public ResponseResult handleValidationException(MethodArgumentNotValidException e) {
		log.error("method argument not valid exception ", e);
		return ResponseResult.error(CommonExcpConstants.ERROR_CODE_BAT_REQUEST, CommonExcpConstants.ERROR_MSG_BAT_REQUEST);
	}

	/**
	 * 404-NOT_FOUND
	 * @param e
	 * @return
	 */
	@ResponseStatus(HttpStatus.NOT_FOUND)
	@ExceptionHandler(NoHandlerFoundException.class)
	public ResponseResult handlerNotFoundException(NoHandlerFoundException e) {
		log.error("no handler found exception ", e);
		return ResponseResult.error(CommonExcpConstants.ERROR_CODE_NOT_FOUNT, CommonExcpConstants.ERROR_MSG_NOT_FOUNT);
	}

	/**
	 * 405 - method Not allowed HttpRequestMethodNotSupportedException
	 * 是ServletException 的子类，需要Servlet API 支持
	 * @param e
	 * @return
	 */
	@ResponseStatus(HttpStatus.METHOD_NOT_ALLOWED)
	@ExceptionHandler(HttpRequestMethodNotSupportedException.class)
	public ResponseResult handleHttpRequestMethodNotSupportedException(HttpRequestMethodNotSupportedException e) {
		log.error("http request method not supported exception ", e);
		return ResponseResult.error(CommonExcpConstants.ERROR_CODE_METHOD_NOT_SUPPORTED, CommonExcpConstants.ERROR_MSG_METHOD_NOT_SUPPORTED);
	}

	/**
	 * 415-Unsupported Media
	 * Type.HttpMediaTypeNotSupportedException是ServletException的子类，需要Serlet
	 * API支持
	 * @param e
	 * @return
	 */
	@ResponseStatus(HttpStatus.UNSUPPORTED_MEDIA_TYPE)
	@ExceptionHandler({ HttpMediaTypeNotSupportedException.class })
	public ResponseResult handleHttpMediaTypeNotSupportedException(Exception e) {
		log.error("http media type not supported exception ", e);
		return ResponseResult.error(CommonExcpConstants.ERROR_CODE_UNSUPPORTED_MEDIA_TYPE, CommonExcpConstants.ERROR_MSG_UNSUPPORTED_MEDIA_TYPE);
	}


	/**
	 * 500 - Internal Server Error
	 * @param e
	 * @return
	 */
	@ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
	@ExceptionHandler(Exception.class)
	public ResponseResult handleException(Exception e) {
		log.error("internal server error exception ", e);
		return ResponseResult.error(CommonExcpConstants.ERROR_CODE, e.getMessage());
	}

}
